import 'package:enya_app_upgrade/app_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'enya_app_upgrade_platform_interface.dart';

/// An implementation of [EnyaAppUpgradePlatform] that uses method channels.
class MethodChannelEnyaAppUpgrade extends EnyaAppUpgradePlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('enya_app_upgrade');

  @override
  Future<AppInfo> getAppInfo() async {
    final result = await methodChannel.invokeMethod('getAppInfo');
    return AppInfo(
      versionName: result['versionName'],
      versionCode: result['versionCode'],
      packageName: result['packageName'],
    );
  }

  @override
  Future<void> toAppStore(String appId) async {
    await methodChannel.invokeMethod<void>('toAppStore', {'id': appId});
  }

  @override
  Future<String> getApkDownloadPath(String version) async {
    final result = await methodChannel.invokeMethod<String>(
      'getApkDownloadPath',
      {'version': version},
    );
    return result ?? '';
  }

  @override
  Future<void> installAppForAndroid(String path) async {
    await methodChannel.invokeMethod<void>('install', {'path': path});
  }
}
